package edu.ynu.se.xiecheng.achitectureclass.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import edu.ynu.se.xiecheng.achitectureclass.entity.Shop;
import edu.ynu.se.xiecheng.achitectureclass.entity.User;
import edu.ynu.se.xiecheng.achitectureclass.entity.vo.Result;
import edu.ynu.se.xiecheng.achitectureclass.mapper.ShopMapper;
import edu.ynu.se.xiecheng.achitectureclass.mapper.UserMapper;
import edu.ynu.se.xiecheng.achitectureclass.service.ShopService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 *
 * @author laruui
 * @since 2023-12-07
 */
@Slf4j
@Service
public class ShopServiceImpl extends ServiceImpl<ShopMapper, Shop> implements ShopService {

    @Resource
    private ShopMapper shopMapper;

    @Resource
    private UserMapper userMapper;

    @Override
    public Result pageShop(int current, int size, String userId) {
        try {
            Page<Shop> shopPage = new Page<>(current, size);
            LambdaQueryWrapper<Shop> shopLambdaQueryWrapper = new LambdaQueryWrapper<>();
            User user = userMapper.selectById(userId);
            String flag = user.getFlag();
            if ("business".equals(flag)) {
                shopLambdaQueryWrapper.eq(Shop::getUserId, userId);
            }
            Page<Shop> selectedPage = shopMapper.selectPage(shopPage, shopLambdaQueryWrapper);
            return Result.builder().code(200).message("查询成功！").data(selectedPage).build();
        } catch (Exception e) {
            log.info("出现异常！");
            return Result.builder().code(500).message("系统故障").build();
        }
    }

    @Override
    public Result addNewShop(Shop shop) {
        try {
            int row = shopMapper.insert(shop);
            return Result.builder().code(200).message(String.valueOf(row)).build();
        } catch (Exception e) {
            log.info("出现异常！");
            return Result.builder().code(500).message("系统故障").build();
        }
    }
}
